﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class QL_Nguoi_Dung : System.Web.UI.Page
{
    private string Connect = ConfigurationManager.ConnectionStrings["ketnoi"].ConnectionString;
    SqlConnection con;
    SqlDataAdapter ad;
    SqlCommand cmd;
    DataTable dt = new DataTable();
    string sql = "select * from Dang_Nhap where quyen!=1";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["username"].ToString() == "")
        {
            Response.Redirect("Trang_Chu.aspx");
        }
        else
        {
            if (!IsPostBack)
            {
                Hien_Thi();
                btnkluu.Visible = false;
                btbluu.Visible = false;
                btnsua.Visible = true;
                btnxoa.Visible = true;
                btnthem.Visible = true;
                txttendangnhap.ReadOnly = txtmatkhau.ReadOnly = txthovaten.ReadOnly = txtemail.ReadOnly = txtngaysinh.ReadOnly = txtdienthoai.ReadOnly = txtdiachi.ReadOnly = txtcmnd.ReadOnly = true;
            }
        }
       
    }
    public void Hien_Thi()
    {
        con = new SqlConnection(Connect);
        try
        {
            con.Open();
            ad = new SqlDataAdapter(sql, con);
            ad.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
            else
            {
                lblerr.Text = "Không có dữ liệu";
            }
        }
        catch (Exception ex)
        {
            lblerr.Text = "Lỗi: " + ex.Message;
        }
    }
    protected void btbluu_Click(object sender, EventArgs e)
    {
        con = new SqlConnection(Connect);
        try
        {
            con.Open();
            string sql = "";
            cmd = new SqlCommand();
            cmd.Connection = con;
            if (Session["Them"].ToString() == "1")
            {
                sql = "insert into Dang_Nhap(TenDangNhap,MatKhau,Email,CMND,HoVaTen,NgaySinh,DienThoai,DiaChi,Tinh_TP,Quyen) values(N'" + txttendangnhap.Text + "',N'" + txtmatkhau.Text + "',N'" + txtemail.Text + "',N'" + txtcmnd.Text + "',N'" + txthovaten.Text + "','" + txtngaysinh.Text + "','" + txtdienthoai.Text + "',N'" + txtdiachi.Text + "',N'" + DropDownListtinhtp.SelectedValue + "', " + 2 + ")";
            }
            else if (Session["Them"].ToString() == "2")
            {
                sql = "update dang_nhap set matkhau='" + txtmatkhau.Text + "',Email='" + txtemail.Text + "',CMND='" + txtcmnd.Text + "',HoVaTen=N'" + txthovaten.Text + "',NgaySinh='" + txtngaysinh.Text + "',DienThoai='" + txtdienthoai.Text + "',DiaChi=N'" + txtdiachi.Text + "',Tinh_TP=N'" + DropDownListtinhtp.Text + "' where TenDangNhap='" + txttendangnhap.Text + "'";
            }
            cmd.CommandText = sql;
            cmd.CommandType = CommandType.Text;
            int i;
            i = cmd.ExecuteNonQuery();
            if (i > 0)
            {
                lblerr.Text = "Thực hiện thành công";
                btnthem.Visible = true;
                btnsua.Visible = true;
                btnxoa.Visible = true;
                btbluu.Visible = false;
                btnkluu.Visible = false;
                Hien_Thi();
            }
            else
            {
                lblerr.Text = "Thực hiện không thành công";
            }
            con.Dispose();
            con.Close();
        }
        catch (Exception ex)
        {
            lblerr.Text = "lỗi: " + ex.Message;
        }
    }
    protected void btnthem_Click(object sender, EventArgs e)
    {
        txttendangnhap.Text = txtmatkhau.Text = txthovaten.Text = txtemail.Text =txtngaysinh.Text= txtdienthoai.Text = txtdiachi.Text = txtcmnd.Text = DropDownListtinhtp.Text = "";
        txttendangnhap.ReadOnly = txtmatkhau.ReadOnly = txthovaten.ReadOnly = txtemail.ReadOnly=txtngaysinh.ReadOnly = txtdienthoai.ReadOnly = txtdiachi.ReadOnly = txtcmnd.ReadOnly = false;
        lblerr.Text = "";
        btnthem.Visible = false;
        btnsua.Visible = false;
        btnxoa.Visible = false;
        btbluu.Visible = true;
        btnkluu.Visible = true;
        Session["Them"] = 1;
    }
    protected void btnsua_Click(object sender, EventArgs e)
    {
        lblerr.Text = "";
        txttendangnhap.ReadOnly = true;
        txtmatkhau.ReadOnly = txthovaten.ReadOnly = txtemail.ReadOnly = txtngaysinh.ReadOnly = txtdienthoai.ReadOnly = txtdiachi.ReadOnly = txtcmnd.ReadOnly = false;
        btnthem.Visible = false;
        btnsua.Visible = false;
        btnxoa.Visible = false;
        btbluu.Visible = true;
        btnkluu.Visible = true;
        Session["Them"] = 2;
        txtmatkhau.Focus();
    }
    protected void btnxoa_Click(object sender, EventArgs e)
    {
        lblerr.Text = "";
        con = new SqlConnection(Connect);
        try
        {
            con.Open();
            cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText = "delete from Dang_Nhap where tendangnhap = '" + txttendangnhap.Text + "'";
            int i;
            i = cmd.ExecuteNonQuery();
            if (i > 0)
            {
                lblerr.Text = "Đã Xóa Thành Công";
                txttendangnhap.Text = txtmatkhau.Text = txthovaten.Text = txtemail.Text = txtdienthoai.Text = txtdiachi.Text = txtcmnd.Text = DropDownListtinhtp.Text = "";
                Hien_Thi();               
               
            }
            else
            {
                lblerr.Text = "Không xóa được";
            }
            con.Dispose();
            con.Close();

        }
        catch (Exception ex)
        {
            lblerr.Text = "Lỗi:" + ex.Message;
        }
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        Hien_Thi();
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        int idrow = GridView1.SelectedIndex;
        txttendangnhap.Text = GridView1.Rows[idrow].Cells[0].Text;
        txtmatkhau.Text = GridView1.Rows[idrow].Cells[1].Text;
        txtemail.Text = GridView1.Rows[idrow].Cells[2].Text;
        txtcmnd.Text = GridView1.Rows[idrow].Cells[3].Text;
        txthovaten.Text = GridView1.Rows[idrow].Cells[4].Text;
        txtngaysinh.Text = GridView1.Rows[idrow].Cells[5].Text;
        txtdienthoai.Text = GridView1.Rows[idrow].Cells[6].Text;
        txtdiachi.Text = GridView1.Rows[idrow].Cells[7].Text;
        DropDownListtinhtp.Text = GridView1.Rows[idrow].Cells[8].Text;
    }
    protected void btnkluu_Click(object sender, EventArgs e)
    {
        btnthem.Visible = true;
        btnxoa.Visible = true;
        btnsua.Visible = true;
        btbluu.Visible = false;
        btnkluu.Visible = false;
        txttendangnhap.Text = txtmatkhau.Text = txthovaten.Text = txtemail.Text = txtngaysinh.Text = txtdienthoai.Text = txtdiachi.Text = txtcmnd.Text = DropDownListtinhtp.Text = "";
        txttendangnhap.ReadOnly = txtmatkhau.ReadOnly = txthovaten.ReadOnly = txtemail.ReadOnly = txtngaysinh.ReadOnly = txtdienthoai.ReadOnly = txtdiachi.ReadOnly = txtcmnd.ReadOnly = true;
    }
}
